﻿using LeSoft.Application.Bussiness;
using LeSoft.Application.Cache;
using LeSoft.Application.Code;
using LeSoft.Application.Entity;
using LeSoft.Util;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace LeSoft.Application.Web.Areas.PropertyManage.Controllers
{
    public class CS_DeviceMaintanenceBussinessController : MvcControllerBase
    {
        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Index()
        {
            return View();
        }

        [HttpGet]
        [HandlerAuthorize(PermissionMode.Enforce)]
        public ActionResult Form()
        {
            return View();
        }



        /// <summary>
        /// 获取项目树
        /// </summary>
        /// <returns></returns>
        public ActionResult GetTreeJson()
        {
            var data = new WorkBLL().GetWorkTypeList().ToList();
            List<TreeEntity> lst = new List<TreeEntity>();
            foreach (WorkTypeEntity item in data)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = data.Count(t => t.Id == item.ParentId) != 0 || item.ParentId == "0" ? true : false;
                tree.id = item.Id;
                tree.text = item.Name;
                tree.value = item.Code;
                tree.parentId = item.ParentId;
                tree.isexpand = true;
                tree.complete = true;
                tree.hasChildren = hasChildren;
                tree.title = hasChildren == false ? "0" : "1";
                lst.Add(tree);

            }
            var query = new WorkBLL().GetWorkItemList().ToList();
            foreach (WorkItemEntity item in query)
            {
                TreeEntity tree = new TreeEntity();
                bool hasChildren = false;
                tree.id = item.Id;
                tree.text = item.Name;
                tree.value = item.Id;
                tree.parentId = item.Type;
                tree.isexpand = true;
                tree.complete = true;
                tree.img = "fa fa-file-word-o";
                tree.hasChildren = hasChildren;
                lst.Add(tree);
            }
            return Content(lst.TreeToJson());
        }

        public ActionResult GetWeekBillData(string queryJson)
        {
            string Type=string.Empty;
            string year=string.Empty;
            string month=string.Empty;
            string week = string.Empty;
            var data = new WeekPlanBLL().GetList();
            #region 条件筛选
            var queryParam = queryJson.ToJObject();
            if (!queryParam["Type"].IsEmpty())
            {
                Type=queryParam["Type"].ToString();
            }
            if (!queryParam["year"].IsEmpty())
            {
                year = queryParam["year"].ToString();
                data = data.Where(t => t.Year == year);
            }
            if (!queryParam["month"].IsEmpty())
            {
                month = queryParam["month"].ToString();
                string mm = year +( month.Length == 1 ? "0" : "") + month;
                data = data.Where(t => t.Month.Contains(mm));
            }
            if (!queryParam["week"].IsEmpty())
            {
                week = queryParam["week"].ToString();
                data = data.Where(t => t.Week.Contains(week));
            }
            if (!queryParam["OrganizeId"].IsEmpty())
            {
                string OrganizeId = queryParam["OrganizeId"].ToString();
                data = data.Where(t => t.OrganizeId == OrganizeId);
            }
            #endregion
         
            int Y = Convert.ToInt32(year);
            int M = Convert.ToInt32(month);
            int W = Convert.ToInt32(week);
            data = data.Where(e => e.BeginTime.Value.Year == Y).ToList();
            DateTime Date = GetStartDayOfWeeks(Y, 1, 1, 2);
            var qq = new WeekBillBLL().GetWeekBillEntityList("").ToList();
            var query = from D in data
                        select new
                        {
                            Id = D.Id,
                            ItemId = D.ItemId,
                            x = D.Week.Split(',')[0],
                            w = D.Week.Split(',').Length,
                            Title = new WorkBLL().GetWorkItemEntity(D.ItemId) == null ? "" : new WorkBLL().GetWorkItemEntity(D.ItemId).Name,
                            Year = D.Year,
                            Month = D.Month,
                            Week = D.Week,
                            State = D.State == 2 ? "3" : "1",
                            IsEdit = qq.FirstOrDefault(e => e.WeekPlanId == D.Id) != null ? qq.FirstOrDefault(e => e.WeekPlanId == D.Id).State : 0,
                            BeginDate = D.BeginTime,
                            EndDate = D.EndTime,
                            WorkDetail = ""
                            // WorkDetail = GetDetail(D.Id)
                        };
            return Content(query.ToJson());

        }

        /// <summary>
        /// 获取列数据
        /// </summary>
        /// <param name="Type"></param>
        /// <param name="year"></param>
        /// <param name="month"></param>
        /// <param name="week"></param>
        /// <returns></returns>
        public ActionResult GetColumns(string Type, string year, string month, string week)
        {
            int Y = Convert.ToInt32(year);
            int M = Convert.ToInt32(month);
            int W = Convert.ToInt32(week);
            IList<object> ColMs = new List<object>();
            IList<object> Header = new List<object>();
            #region Tab
            var Tab = new
            {
                W1 = "<div class='cs_dv' Id='W1'></div>",
                W2 = "<div class='cs_dv' Id='W2'></div>",
                W3 = "<div class='cs_dv' Id='W3'></div>",
                W4 = "<div class='cs_dv' Id='W4'></div>",
                W5 = "<div class='cs_dv' Id='W5'></div>",
                W6 = "<div class='cs_dv' Id='W6'></div>",
                W7 = "<div class='cs_dv' Id='W7'></div>",
                W8 = "<div class='cs_dv' Id='W8'></div>",
                W9 = "<div class='cs_dv' Id='W9'></div>",
                W10 = "<div class='cs_dv'Id='W10'></div>",
                W11 = "<div class='cs_dv'Id='W11'></div>",
                W12 = "<div class='cs_dv'Id='W12'></div>",
                W13 = "<div class='cs_dv'Id='W13'></div>",
                W14 = "<div class='cs_dv'Id='W14'></div>",
                W15 = "<div class='cs_dv'Id='W15'></div>",
                W16 = "<div class='cs_dv'Id='W16'></div>",
                W17 = "<div class='cs_dv'Id='W17'></div>",
                W18 = "<div class='cs_dv'Id='W18'></div>",
                W19 = "<div class='cs_dv'Id='W19'></div>",
                W20 = "<div class='cs_dv'Id='W20'></div>",
                W21 = "<div class='cs_dv'Id='W21'></div>",
                W22 = "<div class='cs_dv'Id='W22'></div>",
                W23 = "<div class='cs_dv'Id='W23'></div>",
                W24 = "<div class='cs_dv'Id='W24'></div>",
                W25 = "<div class='cs_dv'Id='W25'></div>",
                W26 = "<div class='cs_dv'Id='W26'></div>",
                W27 = "<div class='cs_dv'Id='W27'></div>",
                W28 = "<div class='cs_dv'Id='W28'></div>",
                W29 = "<div class='cs_dv'Id='W29'></div>",
                W30 = "<div class='cs_dv'Id='W30'></div>",
                W31 = "<div class='cs_dv'Id='W31'></div>",
                W32 = "<div class='cs_dv'Id='W32'></div>",
                W33 = "<div class='cs_dv'Id='W33'></div>",
                W34 = "<div class='cs_dv'Id='W34'></div>",
                W35 = "<div class='cs_dv'Id='W35'></div>",
                W36 = "<div class='cs_dv'Id='W36'></div>",
                W37 = "<div class='cs_dv'Id='W37'></div>",
                W38 = "<div class='cs_dv'Id='W38'></div>",
                W39 = "<div class='cs_dv'Id='W39'></div>",
                W40 = "<div class='cs_dv'Id='W40'></div>",
                W41 = "<div class='cs_dv'Id='W41'></div>",
                W42 = "<div class='cs_dv'Id='W42'></div>",
                W43 = "<div class='cs_dv'Id='W43'></div>",
                W44 = "<div class='cs_dv'Id='W44'></div>",
                W45 = "<div class='cs_dv'Id='W45'></div>",
                W46 = "<div class='cs_dv'Id='W46'></div>",
                W47 = "<div class='cs_dv'Id='W47'></div>",
                W48 = "<div class='cs_dv'Id='W48'></div>",
                W49 = "<div class='cs_dv'Id='W49'></div>",
                W50 = "<div class='cs_dv'Id='W50'></div>",
                W51 = "<div class='cs_dv'Id='W51'></div>",
                W52 = "<div class='cs_dv'Id='W51'></div>",
                D1 = "<div class='cs_dv'Id='D1'></div>",
                D2 = "<div class='cs_dv'Id='D2'></div>",
                D3 = "<div class='cs_dv'Id='D3'></div>",
                D4 = "<div class='cs_dv'Id='D4'></div>",
                D5 = "<div class='cs_dv'Id='D5'></div>",
                D6 = "<div class='cs_dv'Id='D6'></div>",
                D7 = "<div class='cs_dv'Id='D7'></div>"

            };
            #endregion
            List<object> list = new List<object>();
            int Weeks = 0;
            int SWeek = 1;
            list.Add(Tab);
            int divide = 0, w = 0;
            switch (Type)
            {
                case "1":
                    for (int i = 1; i < 13; i++)
                    {
                        int j = 0;
                        int Days = Time.GetDaysOfMonth(Y, i);
                        if (i == 1)
                        {//计算1月1号
                            int Week = new DateTime(Y, i, 1).DayOfWeek.GetHashCode();
                            if (Week == 6) Days = Days - 2;
                            if (Week == 0) Days = Days - 1;
                        }
                        j = (Days + divide) / 7;
                        divide = (Days + divide) % 7;
                        if (divide > 2)
                        {
                            j = j + 1;
                            divide = divide - 7;
                        }
                        if (i == 1) ColMs.Add(new { label = "Id", name = "Id", hidden = true });
                        Header.Add(new { startColumnName = "bj" + i, numberOfColumns = j + 1, titleText = i + "月" });
                        ColMs.Add(new { label = "bj", name = "bj" + i, hidden = true });
                        for (int k = 1; k <= j; k++)
                        {
                            ColMs.Add(new { label = "W" + (w + k), name = "W" + (w + k), width = 100, height = 100, align = "center", sortable = false });
                        }
                        w = w + j;
                    }
                    break;
                case "2":
                    for (int i = 1; i < 13; i++)
                    {
                        int j = 0;
                        int Days = Time.GetDaysOfMonth(Y, i);
                        if (i == 1)
                        {//计算1月1号
                            int Week = new DateTime(Y, i, 1).DayOfWeek.GetHashCode();
                            if (Week == 6) Days = Days - 2;
                            if (Week == 0) Days = Days - 1;
                        }
                        j = (Days + divide) / 7;
                        divide = (Days + divide) % 7;
                        if (divide > 2)
                        {
                            j = j + 1;
                            divide = divide - 7;
                        }

                        if (i == M) ColMs.Add(new { label = "bj", name = "bj" + i, hidden = true });
                        for (int k = 1; k <= j; k++)
                        {

                            if (i == M)
                            {
                                Weeks = j;
                                if (k == 1) { SWeek = w + k; };
                                DateTime time = GetStartDayOfWeeks(Y, M, k, 1);
                                ColMs.Add(new { label = "W" + (w + k) + "<br/>" + time.ToString("yyyy-MM-dd") + "&nbsp;—&nbsp;" + (time.AddDays(6)).ToString("yyyy-MM-dd"), name = "W" + (w + k), align = "center", sortable = false });
                            }
                        }
                        w = w + j;
                    }
                    break;
                case "3":
                    for (int i = 1; i < 13; i++)
                    {
                        int j = 0;
                        int Days = Time.GetDaysOfMonth(Y, i);
                        if (i == 1)
                        {//计算1月1号
                            int Week = new DateTime(Y, i, 1).DayOfWeek.GetHashCode();
                            if (Week == 6) Days = Days - 2;
                            if (Week == 0) Days = Days - 1;
                        }
                        j = (Days + divide) / 7;
                        divide = (Days + divide) % 7;
                        if (divide > 2)
                        {
                            j = j + 1;
                            divide = divide - 7;
                        }
                        if (w == W) ColMs.Add(new { label = "bj", name = "bj" + i, hidden = true });
                        for (int k = 1; k <= j; k++)
                        {
                            if (w + k == W)
                            {

                                for (int m = 0; m < 7; m++)
                                {

                                    DateTime time = GetStartDayOfWeeks(Y, M, W, 2);
                                    time = time.AddDays(m);
                                    ColMs.Add(new { label = time.ToString("yyyy-MM-dd") + "<br/>" + Time.GetWeekNameOfDay(time), name = "D" + Convert.ToInt32(m + 1), align = "center", sortable = false });

                                }

                            }
                        }
                        w = w + j;
                    }
                    break;
                default:
                    break;
            }

            var data = new { ColMs = ColMs, Coldata = list, Headdata = Header, WeeksData = Weeks, SWeekData = SWeek };
            return Content(data.ToJson());

        }

        /// <summary>
        /// 获取指定年的指定连续周的起止时间
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="Week">起始周</param>
        /// <param name="Count">周数</param>
        /// <returns></returns>
        public ActionResult GetStimeAndETimeOfWeek(string year, string Week, string Count)
        {
            int Y = Convert.ToInt32(year);
            int W = Convert.ToInt32(Week);
            int C = Convert.ToInt32(Count);
            DateTime STime = GetStartDayOfWeeks(Y, 1, W, 2);
            DateTime ETime = DateTime.Now;
            if (C > 1)
            {
                ETime = STime.AddDays(6 + (C - 1) * 7);
            }
            else
            {
                ETime = STime.AddDays(6 * C);
            }
            var Data = new
            {
                Stime = STime,
                Etime = ETime
            };
            return Content(Data.ToJson());

        }


        public ActionResult getDayOfWeek(string year, string Week, string SDay, string Count)
        {
            int Y = Convert.ToInt32(year);
            int W = Convert.ToInt32(Week);
            int C = Convert.ToInt32(Count);
            int S = Convert.ToInt32(SDay);
            DateTime STime = GetStartDayOfWeeks(Y, 1, W, 2);
            STime = STime.AddDays(S);
            DateTime ETime = STime.AddDays(C).AddSeconds(-1);
            var Data = new
            {
                Stime = STime,
                Etime = ETime
            };
            return Content(Data.ToJson());

        }


        /// <summary>
        /// 获取设备列表
        /// </summary>
        /// <param name="keyValue"></param>
        /// <returns></returns>
        public ActionResult GetPlanDeviceList(string keyValue)
        {
            var data = new DeviceBLL().GetList().ToList();
            var query = new DeviceBLL().GetRelationDeviceItemList(2, null);
            query = query.Where(e => e.ItemId == keyValue).ToList();
            string DeviceLevel = "";
            string ProjectType = "";
            string DeviceTypeId = "";
            if (query.Count() != 0)
            {
                foreach (RelationDeviceItemEntity item in query)
                {
                    if (!DeviceLevel.Contains(item.DeviceLevel))
                    {
                        DeviceLevel += item.DeviceLevel + ",";
                    }
                    if (!ProjectType.Contains(item.ProjectType))
                    {
                        ProjectType += item.ProjectType + ",";
                    }
                    if (!DeviceTypeId.Contains(item.DeviceTypeId))
                    {
                        DeviceTypeId += item.DeviceTypeId + ",";
                    }
                }
                DeviceLevel = DeviceLevel.Contains(",") ? DeviceLevel.Substring(0, DeviceLevel.Length - 1) : "";
                ProjectType = ProjectType.Contains(",") ? ProjectType.Substring(0, ProjectType.Length - 1) : "";
                DeviceTypeId = DeviceTypeId.Contains(",") ? DeviceTypeId.Substring(0, DeviceTypeId.Length - 1) : "";
            }
            data = data.Where(e => DeviceLevel.Contains(e.Level) && DeviceTypeId.Contains(e.Type)).ToList();
            foreach (var item in data)
            {
                item.Type = new DeviceTypeBLL().GetEntity(item.Type).IsEmpty() ? "" : new DeviceTypeBLL().GetEntity(item.Type).Name;
                var dd =new DataItemCache().GetDataItemList("DeviceLevel").FirstOrDefault(e => e.ItemDetailId == item.Level);
                item.Level = dd.IsEmpty() ? "" : dd.ItemName;
            }
            return Content(data.ToJson());
        }

        /// <summary>
        /// 获取计划明细
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="WorkDetail"></param>
        /// <returns></returns>
        public ActionResult GetPalnDetail(string keyValue, string WorkDetail)
        {
            var WEntity = new WorkBLL().GetWorkItemEntity(keyValue);
            if (WorkDetail.IsEmpty())
            {
                var DList = GetPlanDeviceList(keyValue);
                var data = new
                {
                    WEntity = WEntity,
                    DList = DList
                };
                return Content(data.ToJson());
            }
            else
            {
                var d = new DeviceBLL().GetList();
                d = d.Where(e => WorkDetail.Contains(e.Id)).ToList();
                foreach (var item in d)
                {
                    item.Type = new DeviceTypeBLL().GetEntity(item.Type).IsEmpty() ? "" : new DeviceTypeBLL().GetEntity(item.Type).Name;
                    var dd =new DataItemCache().GetDataItemList("DeviceLevel").FirstOrDefault(e => e.ItemDetailId == item.Level);
                    item.Level = dd.IsEmpty() ? "" : dd.ItemName;
                }
                var data = new
                {
                    WEntity = WEntity,
                    DList = d
                };
                return Content(data.ToJson());
            }

        }

        public ActionResult GetPalnEntity(string keyValue, string Year, string SWeek)
        {
            var data = new WeekPlanBLL().GetList().ToList();
            if (!Year.IsEmpty() && !SWeek.IsEmpty())
            {
                var query = data.FirstOrDefault(e => (e.Id == keyValue || e.ItemId == keyValue) && e.Year.Contains(Year) && e.Week.Contains(SWeek) && e.State == 2);
                if (!query.IsEmpty())
                {
                    query.Year = query.Week.Split(',')[0];
                    query.Month = query.Week.Split(',').Length.ToString();
                    return Content(query.ToJson());
                }
                else
                {
                    return null;
                }
            }
            else
            {
                var query = data.FirstOrDefault(e => e.Id == keyValue);
                if (!query.IsEmpty())
                {
                    query.Year = query.Week.Split(',')[0];
                    query.Month = query.Week.Split(',').Length.ToString();
                    return Content(query.ToJson());
                }
                else
                {
                    return null;
                }
            }


        }












        /// <summary>
        /// 保存、提交数据（52周计划）
        /// </summary>
        /// <param name="keyValue"></param>
        /// <param name="Type">1-保存 2-提交</param>
        /// <returns></returns>
        public ActionResult SubmitData(string keyValue, int Type)
        {
            if (keyValue.IsEmpty())
            {
                return Success("提交成功！");
            }
            List<MultipleWeekPlan> lst = ChangeDataToList(keyValue);
            try
            {
                new WeekPlanBLL().SaveForm(Type, lst);
                return Success("提交成功！");
            }
            catch (Exception e)
            {
                return Error("提交失败！");
            }
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="keyValue">主键值</param>
        /// <returns></returns>
        public ActionResult RemovePlanEntity(string keyValue)
        {

            var data = new WeekBillBLL().GetWeekBillEntityList("").ToList();
            WeekBillEntity entity = data.FirstOrDefault(e => e.WeekPlanId == keyValue);
            if (entity != null && entity.State == 0)
            {
                try
                {
                    new WeekPlanBLL().RemoveForm(keyValue);
                    return Success("删除成功");
                }
                catch (Exception)
                {

                    return Error("删除失败");
                }
            }
            else
            {
                return Json("1");
            }

        }









        #region 私有方法


        /// <summary>
        /// 获取指定周的第一天
        /// </summary>
        /// <param name="year">指定年</param>
        /// <param name="month">指定月</param>
        /// <param name="index">指定周</param>
        /// <param name="type"></param>
        /// <returns></returns>
        private DateTime GetStartDayOfWeeks(int year, int month, int index, int type)
        {
            if (type == 1)
            {
                DateTime startMonth = new DateTime(year, month, 1);  //该月第一天
                int dayOfWeek = 7;
                if (Convert.ToInt32(startMonth.DayOfWeek.ToString("d")) > 0)
                    dayOfWeek = Convert.ToInt32(startMonth.DayOfWeek.ToString("d"));  //该月第一天为星期几
                if (dayOfWeek > 3)
                {
                    DateTime startWeek = startMonth.AddDays(1 - dayOfWeek);  //该月第一周开始日期  
                    DateTime startDayOfWeeks = startWeek.AddDays(index * 7);  //index周的起始日期
                    return startDayOfWeeks;
                }
                else
                {
                    DateTime startWeek = startMonth.AddDays(1 - dayOfWeek);  //该月第一周开始日期 
                    DateTime startDayOfWeeks = startWeek.AddDays(index * 7 - 7);
                    return startDayOfWeeks;
                }
            }
            else
            {
                DateTime startMonth = new DateTime(year, 1, 1);  //该年第一天
                int dayOfWeek = 7;
                if (Convert.ToInt32(startMonth.DayOfWeek.ToString("d")) > 0)
                    dayOfWeek = Convert.ToInt32(startMonth.DayOfWeek.ToString("d"));  //该年第一天为星期几
                DateTime startWeek = startMonth.AddDays(1 - dayOfWeek);  //该年第一周开始日期  
                DateTime startDayOfWeeks = startWeek.AddDays(index * 7);  //index周的起始日期
                return startDayOfWeeks;
            }
        }

        public List<DeviceEntity> GetDeviceListByItemId(string keyValue)
        {
            var query = new DeviceBLL().GetRelationDeviceItemList(2, null);
            query = query.Where(e => e.ItemId == keyValue).ToList();
            string DeviceLevel = "";
            string ProjectType = "";
            string DeviceTypeId = "";
            if (query.Count() != 0)
            {
                foreach (RelationDeviceItemEntity item in query)
                {
                    if (!DeviceLevel.Contains(item.DeviceLevel))
                    {
                        DeviceLevel += item.DeviceLevel + ",";
                    }
                    if (!ProjectType.Contains(item.ProjectType))
                    {
                        ProjectType += item.ProjectType + ",";
                    }
                    if (!DeviceTypeId.Contains(item.DeviceTypeId))
                    {
                        DeviceTypeId += item.DeviceTypeId + ",";
                    }
                }
                DeviceLevel = DeviceLevel.Contains(",") ? DeviceLevel.Substring(0, DeviceLevel.Length - 1) : "";
                ProjectType = ProjectType.Contains(",") ? ProjectType.Substring(0, ProjectType.Length - 1) : "";
                DeviceTypeId = DeviceTypeId.Contains(",") ? DeviceTypeId.Substring(0, DeviceTypeId.Length - 1) : "";
            }
            var data = new DeviceBLL().GetList().ToList();
            data = data.Where(e => DeviceLevel.Contains(e.Level) && DeviceTypeId.Contains(e.Type)).ToList();
            return data;
        }
        /// <summary>
        /// 数据转换（提交）
        /// </summary>
        /// <param name="KeyValue"></param> 
        /// <returns></returns> 
        private List<MultipleWeekPlan> ChangeDataToList(string KeyValue)
        {

            List<MultipleWeekPlan> lst = new List<MultipleWeekPlan>();
            string Str = KeyValue.Replace("},{", "}|{");
            Str = Str.Remove(0, 1);
            Str = Str.Remove(Str.Length - 1, 1);
            var array = Str.Split('|');
            for (int i = 0; i < array.Length; i++)
            {
                var json = array[i].ToJObject();
                #region WeekPlanEntity
                WeekPlanEntity entity = new WeekPlanEntity();
                entity.Id = json["Id"].ToString().Length < 15 ? null : json["Id"].ToString();
                entity.ItemId = json["ItemId"].ToString();
                entity.Year = json["Year"].ToString();
                entity.TypeId = new WorkBLL().GetWorkItemEntity(entity.ItemId) == null ? "" : new WorkBLL().GetWorkItemEntity(entity.ItemId).Type;
                string month = Convert.ToDateTime(json["BenginTime"]).Month.ToString();
                if (month.Length == 1)
                {
                    month = json["Year"].ToString() + "0" + month;
                }
                else
                {
                    month = json["Year"].ToString() + month;
                }
                int a = Convert.ToDateTime(json["EndTime"]).Month - Convert.ToDateTime(json["BenginTime"]).Month;
                if (a >= 1)
                {
                    for (int n = 1; n <= a; n++)
                    {
                        if ((Convert.ToDateTime(json["BenginTime"]).Month + n).ToString().Length == 1)
                        {
                            month += "," + json["Year"].ToString() + "0" + Convert.ToInt32(Convert.ToInt32(Convert.ToDateTime(json["BenginTime"]).Month) + n);
                        }
                        else
                        {
                            month += "," + json["Year"].ToString() + Convert.ToInt32(Convert.ToInt32(Convert.ToDateTime(json["BenginTime"]).Month) + n);
                        }

                    }
                }
                entity.Month = month;
                string week = json["sweek"].ToString();
                if (Convert.ToInt32(json["weeks"]) > 1)
                {
                    for (int k = 1; k < Convert.ToInt32(json["weeks"]); k++)
                    {
                        week += "," + Convert.ToInt32(Convert.ToInt32(json["sweek"]) + k);
                    }
                }
                entity.Week = week;
                entity.BeginTime = Convert.ToDateTime(json["BenginTime"]);
                entity.EndTime = Convert.ToDateTime(json["EndTime"]);
                entity.OrganizeId = json["OrganizeId"].ToString();
                entity.EnabledMark = 1;
                #endregion
                #region List<DeviceItemResultEntity>
                List<DeviceItemResultEntity> list = new List<DeviceItemResultEntity>();
                #region Detail
                var data = GetDeviceListByItemId(entity.ItemId);
                foreach (var item in data)
                {
                    WorkItemEntity wentity =  new WorkBLL().GetWorkItemEntity(entity.ItemId);
                    if (!wentity.IsEmpty())
                    {
                        List<Object> obj = ("[" + wentity.ContentDetail + "]").ToObject<List<Object>>();
                        foreach (var ob in obj)
                        {
                            var content = ob.ToString().ToJObject();
                            if (!content["Id"].IsEmpty())
                            {
                                DeviceItemResultEntity dientity = new DeviceItemResultEntity();
                                dientity.DeviceId = item.Id;
                                dientity.ContentId = content["Id"].ToString();
                                dientity.ItemId = entity.ItemId;
                                list.Add(dientity);
                            }
                        }
                    }

                }

                #endregion

                #endregion
                MultipleWeekPlan Mentity = new MultipleWeekPlan();
                Mentity.weekPlan = entity;
                Mentity.DeviceItemResultList = list;
                lst.Add(Mentity);
            }
            return lst;
        }

        #endregion
    }
}
